package cn.tedu.smart.parcel.counter.service.impl;

import cn.tedu.smart.parcel.common.authentication.CurrentPrincipal;
import cn.tedu.smart.parcel.common.enumerator.ServiceCode;
import cn.tedu.smart.parcel.common.ex.ServiceException;
import cn.tedu.smart.parcel.counter.dao.persist.repository.ICabinetRepository;
import cn.tedu.smart.parcel.counter.pojo.entity.Cabinet;
import cn.tedu.smart.parcel.counter.pojo.entity.CabintDetail;
import cn.tedu.smart.parcel.counter.pojo.param.CabinetAddNewParam;
import cn.tedu.smart.parcel.counter.pojo.vo.CabinetStandardVO;
import cn.tedu.smart.parcel.counter.service.ICabinetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CabinetServiceImpl implements ICabinetService {

    @Autowired
    private ICabinetRepository cabinetRepository;


    @Override
    public void addNew(CurrentPrincipal currentPrincipal, CabinetAddNewParam cabinetAddNewParam) {
        // 新增柜机核心逻辑
        // -- 如果柜机被禁用,不允许添加
        Long id = cabinetAddNewParam.getId();
        CabinetStandardVO counterId = cabinetRepository.getStandardById(id);
        if (!counterId.getCabinetStatus().equals(ENABLE_STATE_ON)) {
            String message = "新增柜机失败,选择的柜机被禁用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }

        Cabinet cabinet = new Cabinet();
        BeanUtils.copyProperties(cabinetAddNewParam,cabinet);
        cabinet.setCabinetId(cabinet.getCabinetId());
        int rows = cabinetRepository.insert(cabinet);
        if (rows != 1) {
            String message = "新增柜机失败,服务器忙,请稍后再试!";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }

        CabintDetail cabintDetail = new CabintDetail();
        cabinet.setCabinetId(cabinet.getCabinetId());
        cabintDetail.setDetail(cabinetAddNewParam.getDetail());
        //TODO 添加详情
    }
}
